package org.flexlancer.groups
{
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Button;
	
	import org.flexlancer.Logger.Logger;

	public class GroupManager extends Canvas
	{
		private var _items:Array = new Array();
		private var _selection_x:Number = 0;
		private var _selection_y:Number = 0;
		private var _selection_w:Number = 0;
		private var _selection_h:Number = 0;
		
		public function GroupManager()
		{
			try{
				super();
				this.addEventListener(MouseEvent.MOUSE_DOWN,Me_OnMouseDown);
				this.addEventListener(MouseEvent.MOUSE_UP,Me_OnMouseUp);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function AddGroup(gx:Number,gy:Number,gw:Number,gh:Number):GroupItem{
			try{
				var gInsert:GroupItem = new GroupItem();
				
				gInsert.x = gx;
				gInsert.y = gy;
				gInsert.width = gw;
				gInsert.height = gh;
				
				this._items.push(gInsert);
				this.addChild(gInsert);
				
				return gInsert;
			}catch(err:Error){
				Logger.LogE(err);
			}
			return null;
		}

		public function RemoveGroup(val:GroupItem):void{
			try{
				var index:Number = this._items.indexOf(val);
				this._items.splice(index,1);
				
				this.removeChild(val);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}

		private function Me_OnMouseDown(evt:MouseEvent):void{
			try{
				if ( evt.currentTarget == this ){
					this._selection_x = this.mouseX;
					this._selection_y = this.mouseY;
					
					this.addEventListener(MouseEvent.MOUSE_MOVE,Me_OnMouseMove);				
				}else{
					Alert.show(evt.currentTarget.toString(),"Mouse Down Fail");
				}
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		private function Me_OnMouseUp(evt:MouseEvent):void{
			try{
				this.removeEventListener(MouseEvent.MOUSE_MOVE,Me_OnMouseMove);	
				if ( Math.abs(this._selection_w) >= 5 && Math.abs(this._selection_h) >= 5 ){
					this.graphics.clear();
					var grp:GroupItem = this.AddGroup(this._selection_x,this._selection_y,this._selection_w,this._selection_h);
					this._selection_w = this._selection_h = 0;
					this.callLater(this.CheckHitTest,[grp]);					
				}
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		private function CheckHitTest(grp:GroupItem):void{
			try{
				var boolHitTest:Boolean = false;
				
				for ( var i:Number = 0 ; i < this.numChildren; i ++ ){
					if ( this.getChildAt(i) is Button ){
						var btn:Button = this.getChildAt(i) as Button;
						if ( btn.hitTestObject(grp) ){
							grp.LinkedItem.push(btn);
							boolHitTest = true;
						}
					}
				}
				
				if ( !boolHitTest ) {
					this.RemoveGroup(grp);
				}
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		private function Me_OnMouseMove(evt:MouseEvent):void{
			try{
				this.graphics.clear();
				this._selection_w = this.mouseX - this._selection_x;
				this._selection_h = this.mouseY - this._selection_y;
				this.graphics.beginFill(0xBBCC00);
				this.graphics.drawRect(this._selection_x,this._selection_y,this._selection_w,this._selection_h);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
	}
}